# Streamlined package management
pkgs <- c("dplyr", "tidyr", "haven", "data.table", "stringr", "lubridate", "readxl", 
          "ggplot2", "gghighlight", "patchwork", "rms", "AER", "lmtest", "lme4", 
          "sandwich", "plm", "nnet")

# Function to load or install packages
usePackage <- function(p) {
  for (pkg in p) {
    if (!require(pkg, character.only = TRUE)) {
      install.packages(pkg, repos = "https://cloud.r-project.org/")
      library(pkg, character.only = TRUE)
    }
  }
}

# Load or install all required packages
usePackage(pkgs)

# Figure 4: 2018 Crisis Salience by City-Date
cities <- read_dta("cities.dta")
ggplot(cities, aes(x=date, y=reorder(mupy,date), color = as.numeric(log_exp_norm)) )+
  geom_point(size =1.5) + 
  scale_color_gradientn(colours = rainbow(3)) +
  scale_x_date(date_breaks = "14 day",date_labels = "%Y%m%d") +
  ggtitle("Salience by dates. Vertical lines denote July 21 and August 16.") +
  labs(color = "Salience", y = "City name", x = "Date" )  + 
  geom_vline(xintercept = as.Date("2018-07-21")+0.5) +
  geom_vline(xintercept = as.Date("2018-08-16")+0.5) +
  geom_segment(aes(x = as.Date("2018-07-21")+0.5, y = "Xinyang", xend = as.Date("2018-09-20"), yend = "Xinyang"),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_segment(aes(xend = as.Date("2018-07-21")+0.5, yend = "Xinyang", x = as.Date("2018-09-20"), y = "Xinyang"),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_label(aes(x = as.Date("2018-08-30"), y = "Xi'an", label = "Correction analysis"), color = "black", fill = "white"  ) +
  geom_segment(aes(x = as.Date("2018-07-01"), y = "Linfen", xend = as.Date("2018-08-16")+0.5, yend = "Linfen"),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_segment(aes(xend = as.Date("2018-07-01"), yend = "Linfen", x = as.Date("2018-08-16")+0.5, y = "Linfen"),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_label(aes(x = as.Date("2018-08-01"), y = "Baotou", label = "Crisis analysis"), color = "black", fill = "white"  )+
  theme(legend.position = c(0.8, 0.4))
ggsave("Salience_date_cities.pdf", width = 20, height = 20, units = "cm")

# Appendix 6. 2018 Crisis Salience Daily Average, 11 July–12 September 2018
national_data <- read_excel("NationalSalience.xlsx") %>%
  mutate(date = ymd(paste("2018", month(date), day(date), sep = "-")))
ggplot(national_data, aes(x = date, y = log_exp)) +
  geom_point() + # Add points to the plot
  geom_line() + # Add line to connect the points
  scale_x_date(date_breaks = "14 day", date_labels = "%Y%m%d") +
  labs(x = "Date", y = "Baidu Index (log)", title = "Salience level by dates. Vertical lines denote July 21 and August 16.") +
  geom_vline(xintercept = as.Date("2018-07-21"), linetype = "dashed") + # Make this vertical line dashed
  geom_vline(xintercept = as.Date("2018-08-16"), linetype = "dashed") + # Make this vertical line dashed
  geom_segment(aes(x = as.Date("2018-07-21")+0.5, y = 2.5, xend = as.Date("2018-09-12"), yend = 2.5),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_segment(aes(xend = as.Date("2018-07-21")+0.5, yend = 2.5, x = as.Date("2018-09-12"), y = 2.5),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_label(x = as.Date("2018-08-20"), y = 2.5, label = "Correction analysis", color = "black", fill = "white") +
  geom_segment(aes(x = as.Date("2018-07-11"), y = 5, xend = as.Date("2018-08-16")+0.5, yend = 5),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_segment(aes(xend = as.Date("2018-07-11"), yend = 5, x = as.Date("2018-08-16")+0.5, y = 5),
               arrow = arrow(length = unit(0.2, "cm")), color = "black") +
  geom_label(x = as.Date("2018-07-30"), y = 5, label = "Crisis analysis", color = "black", fill = "white") +
  theme_minimal()
ggsave("Salience_date.pdf", width = 20, height = 15, units = "cm")
